package cn.aticow.springboot.controller;

import cn.aticow.springboot.bean.Task;
import cn.aticow.springboot.mapper.PageMapper;
import cn.aticow.springboot.service.TaskService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;

@Controller//该标注表明该类是一个控制层[该层直接与网页上的界面进行信息交互]
public class TaskController {

    @Autowired//自动注入业务类[可以直接使用]
    TaskService taskService;
    @Autowired
    PageMapper pageMapper;
    /*此flag用于表示是否点击了按钮*/
    int i=0;
    @RequestMapping("/show")
    public String show(@RequestParam(defaultValue = "1",value = "page") int page,@RequestParam(defaultValue = "10") int pageSize, Model model){
        /*PageHelper.startPage(page,pageSize)语句后面必须紧跟着sql查询语句*/
        /*
        * 当page不是1时显示的页面就是page,并把当前页存放至数据库
        * 当page为1时再加一个判断条件，看一下是否是按钮触发的
        * */
        if (page!=1||(i!=2&&i!=3)){
            PageHelper.startPage(page,pageSize);
            List<Task> lists=taskService.findAllTask();
            PageInfo pageInfo=new PageInfo(lists);
            model.addAttribute("pageInfo",pageInfo);
            //更改当前所在页码
            pageMapper.updatePageNum(String.valueOf(page));
        }else{
            if (i==2||i==3){//表示此次事件是由按钮触发的
                //定位当前所在页
                String pageNumber=pageMapper.findPageNum();
                int pageNow=Integer.parseInt(pageNumber);
                PageHelper.startPage(pageNow,pageSize);
                List<Task> lists=taskService.findAllTask();
                PageInfo pageInfo=new PageInfo(lists);
                model.addAttribute("pageInfo",pageInfo);
                i=0;//复位此事件
            }
        }
        return "list";
    }
    @RequestMapping("/manual")
    public String manualFinished(@RequestParam(value = "ids")List<String> lists,Model model){
        i=2;
        System.out.println("========点击手动完成==========");
        for(int i=0;i<lists.size();i++){
            String ids=lists.get(i);
            Integer id=Integer.parseInt(ids);
            Task task=new Task();
            task.setId(id);
            task.setStatus(38);
            taskService.updateStatusById(task);
        }
        return "redirect:show";
    }

    @RequestMapping("/fail")
    public String manualFailed(@RequestParam(value = "ids")List<String> lists){
        i=3;
        System.out.println("========点击手动失败==========");
        for(int i=0;i<lists.size();i++){
            String ids=lists.get(i);
            Integer id=Integer.parseInt(ids);
            Task task=new Task();
            task.setId(id);
            task.setStatus(39);
            taskService.updateStatusById(task);
        }
        return "redirect:show";
    }

    //        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        long currentTime = System.currentTimeMillis();
//        Date date = new Date(currentTime);
//        Timestamp timestamp = Timestamp.valueOf(sdf.format(date));
}
